import axios from 'axios';
import store from '@/store';
import router from '@/router'

const axiosInstance = axios.create({
    timeout: 30000,
    withCredentials: false
})

axiosInstance.interceptors.request.use(
    config => {  
        // 在请求头中添加自定义字段  
        config.headers['X-SYSTEM'] = 'saas'; 
        config.headers['X-Access-Token'] = sessionStorage.getItem('XAccessToken')
        return config;  
      },  
      error => {  
        // 对请求错误做些什么  
        return Promise.reject(error);  
      }  
)

axiosInstance.interceptors.response.use(
    response => {
      const res = response.data
      if (res.code === 1011){
        console.log("登录过期")
        sessionStorage.setItem('XAccessToken','')
        location.assign('http://dev.i3vsoft.com:10100/saas-dev/enterprise/#/login?redirect='+encodeURIComponent(sessionStorage.getItem('redirect'))+'&title=百思商城商家后台&productId=1772514752703082498')
        return Promise.reject(response);
      }
      if(res.code === 405){
        location.assign('http://dev.i3vsoft.com:10100/saas-dev/enterprise/#/login?redirect='+encodeURIComponent(sessionStorage.getItem('redirect'))+'&title=百思商城商家后台&productId=1772514752703082498')
        return Promise.reject(response);
      }
      return response;
    },
    error => {
        // 对响应错误做点什么
        return Promise.reject(error);
    }
  )


export default axiosInstance